package com.mozai.shopping.service.impl;


import com.mozai.shopping.mapper.ActivityMapper;
import com.mozai.shopping.pojo.Activity;
import com.mozai.shopping.pojo.PageResp;
import com.mozai.shopping.pojo.http.ActivityQueryRep;
import com.mozai.shopping.service.ActivityService;
import com.mozai.shopping.utils.bean.CommonQueryBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author wj183
* @description 针对表【activity】的数据库操作Service实现
* @createDate 2024-09-29 08:10:24
*/
@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 活动新增
     *
     * @param activity
     */
    @Override
    public void insertActivity(Activity activity) {
        activityMapper.insert(activity);
    }

    /**
     * 删除活动
     *
     * @param activity
     * @return
     */
    @Override
    public int deleteActivity(Activity activity) {
        Activity activity1 = activityMapper.selectById(activity.getId());
        Integer delFlag = activity1.getDelFlag();
        if (delFlag == 0) {
            activity.setDelFlag(1);
            return activityMapper.updateById(activity);
        }
        return 0;
    }


    /**
     * 修改活动
     *
     * @param activity
     * @return
     */
    @Override
    public int modifyActivity(Activity activity) {
        if (activityMapper.selectById(activity.getId()).getDelFlag() == 0) {
            activity.setDelFlag(activityMapper.selectById(activity.getId()).getDelFlag());
            return activityMapper.updateById(activity);
        }
        return 0;
    }


    /**
     * 分页活动查询
     * @param activity
     * @param commonQueryBean
     * @return
     */
    @Override
    public PageResp<Activity> queryActivityList(Activity activity, CommonQueryBean commonQueryBean) {
        if(commonQueryBean!=null && commonQueryBean.getPageNum()!=null && commonQueryBean.getPageSize()!=null){
            commonQueryBean.setStart((commonQueryBean.getPageNum()-1)*commonQueryBean.getPageSize());
        }
        List<Activity> activityResult = activityMapper.list4Page(activity,commonQueryBean);
        //查询总条数
        int count = activityMapper.count(activity);
        commonQueryBean.setTotal(count);
        commonQueryBean.setTotalPage(CommonQueryBean.calculateTotalPage(commonQueryBean));
        PageResp<Activity> activityPageResp = new PageResp<>();
        activityPageResp.setList(activityResult);
        activityPageResp.setPageNum(commonQueryBean.getPageNum());
        activityPageResp.setPageSize(commonQueryBean.getPageSize());
        activityPageResp.setTotalPage(commonQueryBean.getTotalPage());
        activityPageResp.setStart(commonQueryBean.getStart());
        activityPageResp.setTotal(commonQueryBean.getTotal());
        return activityPageResp;
    }


    /**
     * 查询单个活动
     * @param activityQueryRep
     * @return
     */
    @Override
    public Activity queryActivity(ActivityQueryRep activityQueryRep) {
        Activity activity = activityMapper.selectById(activityQueryRep.getId());
        // 检查活动是否存在
        if (activity == null) {
            // 可以选择记录日志
            log.warn("活动不存在，ID: {}", activityQueryRep.getId());
            return null;
        }

        // 检查删除标志
        if (activity.getDelFlag() != 0) {
            log.warn("活动已被删除，ID: {}", activityQueryRep.getId());
            return null;
        }
        return activity;
    }
}




